home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-21 | 3.2 KB | 167 lines | [TEXT/CWIE] |
- //Copyright (c) 1997 Brian and Aidan Cully
- //All rights reserved
-
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <types.h>
- #include <QuickDraw.h>
- #include <Events.h>
- #include "conic.h"
-
- extern p3d rotpoint(p3d,float);
-
- TConic::TConic()
- {
- mAConic = 0;
- mConic = 0;
- mIntersect = 0l;
- }
-
- TConic::~TConic()
- {
- if( mConic )
- delete mConic;
- if( mAConic )
- delete mAConic;
- if( mIntersect )
- delete mIntersect;
- }
-
- void TConic::Init()
- {
- int surf;
- p3d cpoint,bcpoint;
-
- mConic = new p3d[CONICPARTS+2];
- mAConic = new p3d[CONICPARTS+2];
- mIntersect = new int[CONICPARTS+2];
- cpoint.x = -6;
- cpoint.y = 0;
- cpoint.z = 6;
-
- bcpoint.x = 6;
- bcpoint.y = 0;
- bcpoint.z = -6;
-
- for (surf=0;surf<=CONICPARTS;surf+=2) {
- mAConic[surf] = rotpoint(cpoint,(((float)360)/CONICPARTS*surf));
- mAConic[surf+1] = rotpoint(bcpoint,(((float)360)/CONICPARTS*surf));
- }
- }
-
- void TConic::CalcIntersect( const TPlane &plane ) {
- float mu,temp;
- int lineno,s;
- p3d p1,p2,n,d;
-
- n = plane.mPNorm;
- s = n.y*plane.mPlane[0].y + n.z*plane.mPlane[0].z;
- mNumInt = 0;
-
- for(lineno=0;lineno<=CONICPARTS;lineno+=2) {
- p1 = mAConic[lineno];
- p2 = mAConic[lineno+1];
- mConic[lineno] = p1;
- mConic[lineno+1] = p2;
- d.x = p1.x - p2.x;
- d.y = p1.y - p2.y;
- d.z = p1.z - p2.z;
-
- temp = n.y*d.y + n.z*d.z;
- if (temp != 0) {
- mu = (s - (n.x*p1.x + n.y*p1.y + n.z*p1.z))/temp;
- //mu *= -1;
- if ((mu >= -0.5) && (mu <= 0)) {
- mConic[lineno].x = p1.x + mu*d.x;
- mConic[lineno].y = p1.y + mu*d.y;
- mConic[lineno].z = p1.z + mu*d.z;
- mIntersect[mNumInt] = lineno;
- mNumInt++;
- }
- if ((mu <= -0.5) && (mu >= -1)) {
- mConic[lineno+1].x = p1.x + mu*d.x;
- mConic[lineno+1].y = p1.y + mu*d.y;
- mConic[lineno+1].z = p1.z + mu*d.z;
- mIntersect[mNumInt] = lineno + 1;
- mNumInt++;
- }
- }
- }
- }
-
- TPlane::TPlane():
- mPAng(0)
- {
- mAPlane = 0l;
- mPlane = 0l;
- mPDis = 2;
- }
-
- TPlane::~TPlane()
- {
- if( mAPlane )
- delete mAPlane;
- if( mPlane )
- delete mPlane;
- }
-
- void TPlane::Init()
- {
- mAPlane = new p3d[4];
- mPlane = new p3d[4];
-
-
- mAPlane[0].x = -6; mAPlane[0].y = -6; mAPlane[0].z = mPDis;
- mAPlane[1].x = 6; mAPlane[1].y = -6; mAPlane[1].z = mPDis;
- mAPlane[2].x = 6; mAPlane[2].y = 6; mAPlane[2].z = mPDis;
- mAPlane[3].x = -6; mAPlane[3].y = 6; mAPlane[3].z = mPDis;
- }
-
- void TPlane::SetDistance( float dis )
- {
- int surf;
-
- mPDis = dis;
- if( mPDis < 0.25 )
- mPDis = 0.25;
- else if( mPDis > 7 )
- mPDis = 7;
- for( surf = 0; surf < 4; surf++ )
- mAPlane[surf].z = mPDis;
- RotPlane();
- }
-
- void TPlane::RotPlane() {
- int vert;
-
- for( vert=0; vert<4; vert++ ) {
- mPlane[vert].x = mAPlane[vert].x;
- mPlane[vert].y = GetCosAngle()*mAPlane[vert].y-GetSinAngle()*mAPlane[vert].z;
- mPlane[vert].z = GetSinAngle()*mAPlane[vert].y+GetCosAngle()*mAPlane[vert].z;
- }
- CalcNormal();
- }
-
- void TPlane::SetAngle( float ang ) {
- mPAng.SetAngle( ang );
- RotPlane();
- }
-
- void TPlane::CalcNormal() {
- p3d v1, v2, n;
-
- v1.x = mPlane[0].x - mPlane[1].x;
- v1.y = mPlane[0].y - mPlane[1].y;
- v1.z = mPlane[0].z - mPlane[1].z;
-
- v2.x = mPlane[0].x - mPlane[2].x;
- v2.y = mPlane[0].y - mPlane[2].y;
- v2.z = mPlane[0].z - mPlane[2].z;
-
- n.x = v1.y*v2.z - v1.z*v2.y;
- n.y = v1.z*v2.x - v1.x*v2.z;
- n.z = v1.x*v2.y - v1.y*v2.x;
- mPNorm = n;
- }